﻿/// <summary>
/// 将指定文件夹下的文件 Name-Path信息制作成Json文件
/// 2020/5/21： TODO 暂时只加载TextAsset文件
/// </summary>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
using System.IO;
using Newtonsoft.Json;
using System.Linq;
namespace CommonLib.Editor
{

	public class GetFilesNameEditorWindow : EditorWindow
	{
		[MenuItem("Assets/Json/FilesName2Path")]
		static public void CreateWindow()
		{
			GetFilesNameEditorWindow window = EditorWindow.GetWindow<GetFilesNameEditorWindow>();
			window.Show();
			window.Init();
		}




		private void OnGUI()
		{
			float windowWidth = position.width;
			float widdowHeight = position.height;
			//不进行根目录的采集
			if (directory == "Assets")
				return;

			ScopeValue = EditorGUILayout.BeginScrollView(ScopeValue,GUILayout.Width(windowWidth),GUILayout.Height(widdowHeight));	
			EditorGUILayout.BeginVertical();
			string tempFilter = EditorGUILayout.TextField("刷选字符", filter);
			if(tempFilter != filter)
			{
				//重置列表
				filter = tempFilter;
				GetAllSelect();
			}

			//选择截取目录
			EditorGUILayout.BeginHorizontal();
			cutoutPath = EditorGUILayout.TextField("相对位置的根目录", cutoutPath);
			if(GUILayout.Button("Select", GUILayout.Width(0.1f * windowWidth)))
			{
				string st = System.Environment.CurrentDirectory.Replace(@"\", @"/");
				cutoutPath = EditorUtility.OpenFolderPanel("Select", directory, "").Replace(st+@"/", "");
				Debug.Log(cutoutPath);
			}
			EditorGUILayout.EndHorizontal();

			EditorGUILayout.Space();
			EditorGUILayout.Space();


			//筛选出的列表
			#region List
			EditorGUILayout.BeginVertical();
			EditorGUILayout.LabelField("Assets", new GUIStyle("WhiteBoldLabel") { alignment = TextAnchor.MiddleCenter });
			/*
			foreach (var item in lstAssets)
			{
				EditorGUILayout.LabelField(item.name);
				EditorGUILayout.LabelField(AssetDatabase.GetAssetPath(item));
				if(GUILayout.Button("X"))
				{
					lstAssets.Remove(item);
					lstAssetsFilter.Add(item);
				}
			}
			*/
			for (int i = 0; i < lstAssets.Count; i++)
			{
				var item = lstAssets[i];
				EditorGUILayout.BeginHorizontal();
				EditorGUILayout.LabelField(item.name, GUILayout.Width( 0.2f * windowWidth));
				EditorGUILayout.LabelField(AssetDatabase.GetAssetPath(item), GUILayout.Width(0.6f * windowWidth));
				if (GUILayout.Button("X", GUILayout.Width(0.1f * windowWidth)))
				{
					lstAssets.Remove(item);
					lstAssetsFilter.Add(item);
					i--;
				}
				EditorGUILayout.EndHorizontal();
			}

			EditorGUILayout.LabelField("Assets Filter", new GUIStyle("WhiteBoldLabel") { alignment = TextAnchor.MiddleCenter });
			/*
			foreach (var item in lstAssetsFilter)
			{
				EditorGUILayout.LabelField(item.name);
				EditorGUILayout.LabelField(AssetDatabase.GetAssetPath(item));
				if (GUILayout.Button("Add"))
				{
					lstAssets.Add(item);
					lstAssetsFilter.Remove(item);
				}
			}
			*/
			for (int i = 0; i < lstAssetsFilter.Count; i++)
			{
				var item = lstAssetsFilter[i];
				EditorGUILayout.BeginHorizontal();
				EditorGUILayout.LabelField(item.name, GUILayout.Width(0.2f * windowWidth));
				EditorGUILayout.LabelField(AssetDatabase.GetAssetPath(item), GUILayout.Width(0.6f * windowWidth));
				if (GUILayout.Button("Add", GUILayout.Width(0.1f * windowWidth)))
				{
					lstAssets.Add(item);
					lstAssetsFilter.Remove(item);
					i--;
				}
				EditorGUILayout.EndHorizontal();
			}

			EditorGUILayout.EndVertical();

			#endregion

			GUILayout.FlexibleSpace();
			if(GUILayout.Button("Save"))
			{
				SaveLst(EditorUtility.SaveFilePanel("Save", directory, filter, "txt"));
			}
			EditorGUILayout.EndVertical();
			EditorGUILayout.EndScrollView();
		}


		private void Init()
		{
			directory = EditorUtil.GetSelectFolder();
			lstAssets = new List<TextAsset>();
			lstAssetsFilter = new List<TextAsset>();
			minSize = new Vector2(700, 500);
		}

		protected void GetAllSelect()
		{
			lstAssets.Clear();
			lstAssetsFilter.Clear();
			EditorUtil.GetAllAssets<TextAsset>(directory, filter, ref lstAssets);
		}
		protected void SaveLst(string path) {
			if (null == path) return;
			List<Name2Path> pairsList = new List<Name2Path>();
			foreach (var asset in lstAssets)
			{
				pairsList.Add(new Name2Path()
				{
					Name = asset.name,
					Path = AssetDatabase.GetAssetPath(asset).Replace(cutoutPath + @"/", "")
				});
			}
			string jsonString = JsonConvert.SerializeObject(pairsList);
			using (StreamWriter sw = new StreamWriter(new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write)))
			{
				sw.Write(jsonString);
			}
		}



		protected string directory;
		protected string filter;
		protected string cutoutPath;
		protected List<TextAsset> lstAssets;
		protected List<TextAsset> lstAssetsFilter;
		protected AssetType assetType = AssetType.TextAsset;

		Vector2 ScopeValue = Vector2.zero;

		protected enum AssetType
		{
			TextAsset = 0,
			Audio = 1,
			Movie = 2,
			GameObject = 3

		}	
	}
}